import pymysql
from dotenv import load_dotenv
import os

# 加载环境变量
load_dotenv()

try:
    # 连接到zpsjfx数据库
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='123456',
        database='zpsjfx'
    )
    
    with conn.cursor() as cursor:
        # 查询testuser用户
        cursor.execute('SELECT * FROM users WHERE username = %s', ('testuser',))
        result = cursor.fetchone()
        print('用户testuser:', result)
        
        # 查询所有用户
        cursor.execute('SELECT * FROM users')
        all_users = cursor.fetchall()
        print("\n所有用户:")
        for user in all_users:
            print(f"  - ID: {user[0]}, 用户名: {user[1]}, 邮箱: {user[3]}")
    
    conn.close()
    
except Exception as e:
    print(f"数据库查询失败: {e}")